package pearson.automation.forms;

import java.util.ArrayList;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import framework.BaseForm;
import framework.elements.ElementList;
import framework.elements.Label;

public class ModuleContents extends BaseForm {
    public static ElementList sections = null;
    public static ElementList moduleAssets = null;
    public static ElementList moduleAssetTitles = null;
    //
    
    public static Label moduleName = new Label(
            By.xpath("//li[contains(@class,'module unlocked')]//h2[@class='module-title']"), 
            "Module title");
    
    public ModuleContents(String module) {
        super(moduleName.getLocator(), "Module");
        moduleAssets = new ElementList(
                By.xpath("//li[@class='module unlocked']"), "Module asset titles", 
                "//div[contains(.,'" + module + "')]//a[@class='asset-title'][contains(.,'%s')]");

        sections = new ElementList(
                By.xpath("//li[@class='module unlocked']"), "Section titles", 
                "//div[contains(.,'" + module + "')]/ol/li/a[@class='section-title'][contains(.,'%s')]");
    }

    public boolean hasAsset(String assetName) {
        return moduleAssets.hasListElement(assetName);
    }
    

    
    public ArrayList<String> getAssetNames() {
        ArrayList<String> result = new ArrayList<String>();
        for (WebElement el: moduleAssetTitles.getListElements()) {
            result.add(el.getText());
        }
        return result;
    }
    
    public boolean hasSection(String sectionTitle) {
        return sections.hasListElement(sectionTitle);
    }
    
    public SectionContents openSection(String sectionTitle) {
        sections.selectListElement(sectionTitle);
        return new SectionContents();
    }

    public void openAssetDetails(String assetName) {
        moduleAssets.selectListElement(assetName);
    }
}
